package wstrader.task.trade.reports;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

import pc.system.*;
import wstrader.task.GenReport;
import wstrader.task.trade.*;
import pc.util.*;

public class NetDiffRisk extends TradeTask
{
  public NetDiffRisk(Tasks owner)
  {
    super(owner,"net-diff-risk");
  }
  
  public long doTask()
  {
  	long Ret =0;
    int task_id = this.getRequest().getIntValue("task_id");
    switch (task_id)
	{
    	case 5: //Report
    		getResponse().setReport(new PrintNDRReport(this).generate());
    		break;  
    		
        	default : //View only
            break;	
	}
  	
  	return Ret;
  }  
  
  public ResultSet getGradeList(String OrdBy)
  {
  	String Sql = "select id,name from grade_master order by "+OrdBy;
  	return getSQLCommand().executeQuery(Sql);
  }
  

  public float getStock(int Qid, int Gid)
  {
  		float Stk =0;
  		float RecdTon =0;
  		float InvTon=0;
  		RecdTon = this.getTotStockTon(Qid,Gid);
  		InvTon = this.getTotInvTon(Qid,Gid);
  		Stk = RecdTon - InvTon;
  		return Stk;
  }
  
  public float getStockAvgDiff(int Qid, int Gid)
  {
  		float StkDiff =0;
  		
  		StkDiff = this.GetStockDiff(Qid,Gid);

  		return StkDiff;
  }
  

 
  public float getAvgDiffNDRReport(String Tbl_name,String FieldName,String FieldName1,int Qid,int Gid, java.sql.Date StDt,java.sql.Date Endt,String CType)
  {
  	float val = 0;
    float valTon = 0;
    float valDiff = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as TotTon,sum(" +  FieldName1 + ") as TotDiff FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date  >= '" + StDt + "' and last_date < '" + Endt + "' and type = '" + CType + "' and active=1";

    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		valTon = rs.getFloat("TotTon");
	  		valDiff = rs.getFloat("TotDiff");
	  		
	  		if (valTon > 0)
	  		{
	  			if (valDiff != 0) val = (valDiff/valTon);
	  		}
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }  
  public float getAvgDiffNDRReport_Before(String Tbl_name,String FieldName,String FieldName1,int Qid,int Gid, java.sql.Date StDt,String CType)
  {
  	float val = 0;
    float valTon = 0;
    float valDiff = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as TotTon,sum(" +  FieldName1 + ") as TotDiff FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date  < '" + StDt + "' and type = '" + CType + "' and active=1";

    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		valTon = rs.getFloat("TotTon");
	  		valDiff = rs.getFloat("TotDiff");
	  		
	  		if (valTon > 0)
	  		{
	  			if (valDiff != 0) val = (valDiff/valTon);
	  		}
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }  
  public float getAvgDiffNDRReport_After(String Tbl_name,String FieldName,String FieldName1,int Qid,int Gid, java.sql.Date EndDt,String CType)
  {
  	float val = 0;
    float valTon = 0;
    float valDiff = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as TotTon,sum(" +  FieldName1 + ") as TotDiff FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date  > '" + EndDt + "' and type = '" + CType + "' and active=1";

    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		valTon = rs.getFloat("TotTon");
	  		valDiff = rs.getFloat("TotDiff");
	  		
	  		if (valTon > 0)
	  		{
	  			if (valDiff != 0) val = (valDiff/valTon);
	  		}
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }    
  public float getSumNDRReport(String Tbl_name,String FieldName,int Qid,int Gid, java.sql.Date StDt,java.sql.Date Endt,String CType)
  {
    float val = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as name FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date  >= '" + StDt + "' and last_date < '" + Endt + "' and type = '" + CType + "' and active = 1";
    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		if (CType.compareTo("P")==0) 
	  		{
	  			val = rs.getFloat("name");
	  			if (val < 0) val=0;
	  		}
	  		if (CType.compareTo("S")==0) 
	  		{
	  			val = rs.getFloat("name");// * -1;
	  			if (val < 0) val=0;
	  			val=val*-1;
	  		}
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }  
  
  public float getSumNDRReport_Before(String Tbl_name,String FieldName,int Qid,int Gid, java.sql.Date StDt,String CType)
  {
    float val = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as name FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date  < '" + StDt + "' and type = '" + CType + "' and active=1";

    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		if (CType.compareTo("P")==0)
	  		{
	  			val = rs.getFloat("name");
	  			if (val < 0) val=0;
	  		}
	  		if (CType.compareTo("S")==0) 
	  		{
	  			val = rs.getFloat("name"); //* -1;
	  			if (val < 0) val=0;
	  			val=val*-1;
	  		}
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }    
  
  public float getSumNDRReport_After(String Tbl_name,String FieldName,int Qid,int Gid, java.sql.Date Endt,String CType)
  {
    float val = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as name FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and last_date > '" + Endt + "' and type = '" + CType + "' and active=1";
    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		if (CType.compareTo("P")==0) val = rs.getFloat("name");
	  		if (CType.compareTo("S")==0) val = rs.getFloat("name") * -1;
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }    
  
  public float getChkTon(String Tbl_name,String FieldName,int Qid,int Gid,String CType)
  {
    float val = 0;
    if (Tbl_name == null || Tbl_name.length() == 0) return 0;
    String sql = "SELECT sum(" +  FieldName + ") as name FROM " + Tbl_name + " where quality_id = " + Qid + " and grade_id = " + Gid + " and type = '" + CType + "' and active=1";

    ResultSet rs = getSQLCommand().executeQuery(sql);

    try {
    	rs.beforeFirst();
	  	if (rs.next()==true)
	  	{
	  		val = rs.getFloat("name");
	  		if (val < 0) val=0;
	  	}
	  	else
	  	{
	  		val=0;
	  		return val;
	  	}
      rs.close();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
      val =0;
      return val;
    }
    return val;
  }   
  
}


class PrintNDRReport extends GenReport
{
	  private NetDiffRisk NDR = null;
	  public PrintNDRReport(Tasks owner)
	  {
	    super(owner,"NetDiffRisk.xls");
	    this.NDR = (NetDiffRisk)owner;
	  }
	  
	  public String generate()
	  {
	  	writeNDRReport();
	  	return export();
	  }	
	  
	  private void writeNDRReport()
	  {	  
	  	int qid = this.getRequest().getIntValue("quality");
	  	if (qid==0) qid =1;
	  	java.sql.Date MonthOne = Utils.strToDate("01-" + Utils.dateToStr(Utils.today()).substring(3));
	  	java.sql.Date MonthTwo = Utils.nextMonth(MonthOne);
	  	java.sql.Date MonthThree = Utils.nextMonth(MonthTwo);	
	  	java.sql.Date MonthFour = Utils.nextMonth(MonthThree);
		java.sql.Date MonthFive = Utils.nextMonth(MonthFour);
		java.sql.Date MonthSix = Utils.nextMonth(MonthFive);
		java.sql.Date MonthSeven = Utils.nextMonth(MonthSix);
		java.sql.Date MonthEight = Utils.nextMonth(MonthSeven);
		java.sql.Date MonthNine = Utils.nextMonth(MonthEight);
		java.sql.Date MonthTen = Utils.nextMonth(MonthNine);
		java.sql.Date MonthEleven = Utils.nextMonth(MonthTen);
		java.sql.Date MonthTwelve = Utils.nextMonth(MonthEleven);									
		java.sql.Date MonthThirteen = Utils.nextMonth(MonthTwelve);
		
		String M_One = Utils.dateToStr(MonthOne).substring(3);
		String M_Two = Utils.dateToStr(MonthTwo).substring(3);
		String M_Three = Utils.dateToStr(MonthThree).substring(3);
		String M_Four = Utils.dateToStr(MonthFour).substring(3);
		String M_Five = Utils.dateToStr(MonthFive).substring(3);
		String M_Six = Utils.dateToStr(MonthSix).substring(3);
		String M_Seven = Utils.dateToStr(MonthSeven).substring(3);
		String M_Eight = Utils.dateToStr(MonthEight).substring(3);
		String M_Nine = Utils.dateToStr(MonthNine).substring(3);
		String M_Ten = Utils.dateToStr(MonthTen).substring(3);
		String M_Eleven = Utils.dateToStr(MonthEleven).substring(3);
		String M_Twelve = Utils.dateToStr(MonthTwelve).substring(3);
		
	  	ResultSet Rs = NDR.getGradeList("name desc");
		float sum_stk_ton = 0;
		float chk_pton =0;
		float chk_ston =0;
		float stk_ton =0;
		float stk_diff =0;
		float Tot1 = 0;
		float Tot2 = 0;
		float Tot3 = 0;
		float Tot4 = 0;	
		float Tot5 = 0;
		float Tot6 = 0;
		float Tot7 = 0;
		float Tot8 = 0;
		float Tot9 = 0;
		float Tot10 = 0;
		float Tot11 = 0;
		float Tot12 = 0;			
		
		float MP_Bef = 0;
		float MP_Aft = 0;
		float MS_Bef = 0;
		float MS_Aft = 0;

		String TFor ="";
		
		String GTFor_Stk = "0+";
		String GTFor_Tot = "0+";
		String GTFor_1 = "0+";
		String GTFor_2 = "0+";
		String GTFor_3 = "0+";
		String GTFor_4 = "0+";
		String GTFor_5 = "0+";
		String GTFor_6 = "0+";
		String GTFor_7 = "0+";
		String GTFor_8 = "0+";
		String GTFor_9 = "0+";
		String GTFor_10 = "0+";
		String GTFor_11 = "0+";
		String GTFor_12 = "0+";
		
		try {
			Rs.beforeFirst();

		int Loop=5;
		
	  	HSSFRow TR = this.getRow((int)0);
	  	HSSFCell TC = TR.getCell((short)0);
	  	HSSFRow R = this.getRow((int)Loop);
	  	HSSFCell C = R.getCell((short)0);
	  	
		this.setCellValue(9,Loop,M_One);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)9);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(11,Loop,M_Two);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)11);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(13,Loop,M_Three);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)13);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(15,Loop,M_Four);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)15);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(17,Loop,M_Five);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)17);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(19,Loop,M_Six);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)19);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(21,Loop,M_Seven);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)21);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(23,Loop,M_Eight);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)23);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(25,Loop,M_Nine);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)25);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(27,Loop,M_Ten);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)27);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(29,Loop,M_Eleven);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)29);
		C.setCellStyle(TC.getCellStyle());
		
		this.setCellValue(31,Loop,M_Twelve);
		R=this.getRow((int)Loop);
		TC = TR.getCell((short)6);
		C = R.getCell((short)31);
		C.setCellStyle(TC.getCellStyle());
		
		Loop=7;
		
		while (Rs.next())
		{
			chk_pton = NDR.getChkTon("contracts","pending_tons",qid,Rs.getInt("id"),"P");
			chk_ston = NDR.getChkTon("contracts","tons-inv_tons",qid,Rs.getInt("id"),"S")*-1;
			stk_ton = NDR.getStock(qid,Rs.getInt("id"));
			stk_diff = NDR.getStockAvgDiff(qid,Rs.getInt("id"));
			
			if ((chk_pton > 0) || (chk_ston != 0) || (stk_ton > 0))
			{
				float MP1 = 0;
				float MPD1 = 0;
				
				float MP2 = 0;
				float MPD2 = 0;
				
				float MP3 = 0;
				float MPD3 = 0;
				
				float MP4 = 0;
				float MPD4 = 0;
					
				float MP5 = 0;
				float MPD5 = 0;
				
				float MP6 = 0;
				float MPD6 = 0;
				
				float MP7 = 0;
				float MPD7 = 0;
				
				float MP8 = 0;
				float MPD8 = 0;
				
				float MP9 = 0;
				float MPD9 = 0;
				
				float MP10 = 0;
				float MPD10 = 0;
				
				float MP11 = 0;
				float MPD11 = 0;
				
				float MP12 = 0;	
				float MPD12 = 0;
				
				float MS1 = 0;
				float MSD1 = 0;
				
				float MS2 = 0;
				float MSD2 = 0;
				
				float MS3 = 0;
				float MSD3 = 0;
				
				float MS4 = 0;
				float MSD4 = 0;
					
				float MS5 = 0;
				float MSD5 = 0;
				
				float MS6 = 0;
				float MSD6 = 0;
				
				float MS7 = 0;
				float MSD7 = 0;
				
				float MS8 = 0;
				float MSD8 = 0;
				
				float MS9 = 0;
				float MSD9 = 0;
				
				float MS10 = 0;
				float MSD10 = 0;
				
				float MS11 = 0;
				float MSD11 = 0;
				
				float MS12 = 0;				
				float MSD12 = 0;
				
				MP_Bef = NDR.getSumNDRReport_Before("contracts","pending_tons", qid, Rs.getInt("id"), MonthOne,"P");
				
				MP1 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthOne, MonthTwo,"P");
				MPD1 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthOne, MonthTwo,"P");
				
				MP2 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthTwo, MonthThree,"P");
				MPD2 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthTwo, MonthThree,"P");
				
				MP3 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthThree, MonthFour,"P");
				MPD3 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthThree, MonthFour,"P");
				
				MP4 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthFour, MonthFive,"P");	
				MPD4 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthFour, MonthFive,"P");	
				
				MP5 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthFive, MonthSix,"P");
				MPD5 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthFive, MonthSix,"P");
				
				MP6 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthSix, MonthSeven,"P");
				MPD6 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthSix, MonthSeven,"P");
				
				MP7 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthSeven, MonthEight,"P");
				MPD7 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthSeven, MonthEight,"P");
				
				MP8 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthEight, MonthNine,"P");
				MPD8 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthEight, MonthNine,"P");
				
				MP9 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthNine, MonthTen,"P");
				MPD9 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthNine, MonthTen,"P");
				
				MP10 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthTen, MonthEleven,"P");
				MPD10 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthTen, MonthEleven,"P");
				
				MP11 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthEleven, MonthTwelve,"P");
				MPD11 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthEleven, MonthTwelve,"P");
				
				MP12 = NDR.getSumNDRReport("contracts", "pending_tons", qid, Rs.getInt("id"), MonthTwelve, MonthThirteen,"P");	
				MPD12 = NDR.getAvgDiffNDRReport("contracts", "pending_tons","pending_tons*differential", qid, Rs.getInt("id"), MonthTwelve, MonthThirteen,"P");	
				
				MP_Aft = NDR.getSumNDRReport_After("contracts", "pending_tons", qid, Rs.getInt("id"), MonthThirteen,"P");

				MS_Bef = NDR.getSumNDRReport_Before("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthOne,"S");			
																								
				MS1 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthOne, MonthTwo,"S");
				MSD1 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthOne, MonthTwo,"S");					
				
				MS2 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthTwo, MonthThree,"S");
				MSD2 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthTwo, MonthThree,"S");
														
				MS3 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthThree, MonthFour,"S");
				MSD3 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthThree, MonthFour,"S");
				
				MS4 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthFour, MonthFive,"S");
				MSD4 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthFour, MonthFive,"S");	
				
				MS5 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthFive, MonthSix,"S");
				MSD5 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthFive, MonthSix,"S");
				
				MS6 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthSix, MonthSeven,"S");
				MSD6 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthSix, MonthSeven,"S");
				
				MS7 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthSeven, MonthEight,"S");
				MSD7 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthSeven, MonthEight,"S");
				
				MS8 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthEight, MonthNine,"S");
				MSD8 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthEight, MonthNine,"S");
				
				MS9 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthNine, MonthTen,"S");
				MSD9 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthNine, MonthTen,"S");
				
				MS10 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthTen, MonthEleven,"S");
				MSD10 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons","(tons-inv_tons)*differential", qid, Rs.getInt("id"), MonthTen, MonthEleven,"S");
				
				MS11 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthEleven, MonthTwelve,"S");
				MSD11 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons", "(tons-inv_tons)*differential",qid, Rs.getInt("id"), MonthEleven, MonthTwelve,"S");
				
				MS12 = NDR.getSumNDRReport("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthTwelve, MonthThirteen,"S");
				MSD12 = NDR.getAvgDiffNDRReport("contracts", "tons-inv_tons", "(tons-inv_tons)*differential",qid, Rs.getInt("id"), MonthTwelve, MonthThirteen,"S");
				
				MS_Aft = NDR.getSumNDRReport_After("contracts", "tons-inv_tons", qid, Rs.getInt("id"), MonthThirteen,"S");
									
				Tot1 = Tot1+(MP_Bef+MS_Bef)+(MP1 + MS1);		
				Tot2 = Tot2+(MP2 + MS2);
				Tot3 = Tot3+(MP3 + MS3);
				Tot4 = Tot4+(MP4 + MS4);
				Tot5 = Tot5+(MP5 + MS5);
				Tot6 = Tot6+(MP6 + MS6);
				Tot7 = Tot7+(MP7 + MS7);
				Tot8 = Tot8+(MP8 + MS8);
				Tot9 = Tot9+(MP9 + MS9);
				Tot10 = Tot10+(MP10 + MS10);
				Tot11 = Tot11+(MP11 + MS11);
				Tot12 =(Tot12+MP_Aft+MS_Aft)+(MP12 + MS12);			
				sum_stk_ton=sum_stk_ton+stk_ton;				
				String ColId = Rs.getInt("id") + "+" + Utils.dateToStr(MonthTwo);
				String ColId2 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthThree);
				String ColId3 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthFour);
				String ColId4 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthFive);
				String ColId5 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthSix);
				String ColId6 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthSeven);
				String ColId7 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthEight);
				String ColId8 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthNine);
				String ColId9 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthTen);
				String ColId10 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthEleven);
				String ColId11 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthTwelve);
				String ColId12 = Rs.getInt("id") + "+" + Utils.dateToStr(MonthThirteen);
				
    			this.setCellValue(4,Loop,Rs.getString("name"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)0);
				C = R.getCell((short)4);
    			C.setCellStyle(TC.getCellStyle());
    			
    			Loop=Loop+1;
    			
    			this.setCellValue(5,Loop,"Purchase");
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)1);
				C = R.getCell((short)5);
    			C.setCellStyle(TC.getCellStyle());  
    			
    			this.setCellValue(7,Loop,stk_ton);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)7);
    			C.setCellStyle(TC.getCellStyle()); 
    			
    			this.setCellValue(8,Loop,stk_diff);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)8);
    			C.setCellStyle(TC.getCellStyle());
 //--------------- Months Starts For Purchase -------------------   			
    			this.setCellValue(9,Loop,MP1+MP_Bef);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)9);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(10,Loop,MPD1);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)10);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(11,Loop,MP2);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)11);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(12,Loop,MPD2);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)12);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(13,Loop,MP3);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)13);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(14,Loop,MPD3);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)14);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(15,Loop,MP4);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)15);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(16,Loop,MPD4);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)16);
    			C.setCellStyle(TC.getCellStyle());
    
    			this.setCellValue(17,Loop,MP5);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)17);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(18,Loop,MPD5);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)18);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(19,Loop,MP6);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)19);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(20,Loop,MPD6);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)20);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(21,Loop,MP7);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)21);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(22,Loop,MPD7);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)22);
    			C.setCellStyle(TC.getCellStyle());    			
    			
    			this.setCellValue(23,Loop,MP8);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)23);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(24,Loop,MPD8);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)24);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(25,Loop,MP9);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)25);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(26,Loop,MPD9);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)26);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(27,Loop,MP10);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)27);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(28,Loop,MPD10);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)28);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(29,Loop,MP11);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)29);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(30,Loop,MPD11);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)30);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(31,Loop,MP12+MP_Aft);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)31);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(32,Loop,MPD12);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)32);
    			C.setCellStyle(TC.getCellStyle());
 
    			TFor = "SUM(H" + (Loop+1) + ",J" + (Loop+1) + ",L" + (Loop+1) + ",N" + (Loop+1) + ",P" + (Loop+1) + ",R" + (Loop+1) + ",T" + (Loop+1) + ",V" + (Loop+1) + ",X" + (Loop+1) + ",Z" + (Loop+1) +",AB" + (Loop+1) + ",AD" + (Loop+1) + ")";
    			this.set_CellFormula(33,Loop,TFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)33);
    			C.setCellStyle(TC.getCellStyle());
    			
    			Loop=Loop+1;
    			
//    			--------------- Months Ends For Purchase -------------------
    			
    			this.setCellValue(5,Loop,"Sales");
    			R=this.getRow((int)Loop);
    			TC = TR.getCell((short)1);
    			C = R.getCell((short)5);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(9,Loop,MS1+MS_Bef);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)9);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(10,Loop,MSD1);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)10);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(11,Loop,MS2);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)11);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(12,Loop,MSD2);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)12);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(13,Loop,MS3);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)13);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(14,Loop,MSD3);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)14);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(15,Loop,MS4);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)15);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(16,Loop,MSD4);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)16);
    			C.setCellStyle(TC.getCellStyle());
    
    			this.setCellValue(17,Loop,MS5);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)17);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(18,Loop,MSD5);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)18);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(19,Loop,MS6);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)19);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(20,Loop,MSD6);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)20);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(21,Loop,MS7);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)21);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(22,Loop,MSD7);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)22);
    			C.setCellStyle(TC.getCellStyle());    			
    			
    			this.setCellValue(23,Loop,MS8);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)23);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(24,Loop,MSD8);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)24);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(25,Loop,MS9);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)25);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(26,Loop,MSD9);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)26);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(27,Loop,MS10);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)27);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(28,Loop,MSD10);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)28);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(29,Loop,MS11);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)29);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(30,Loop,MSD11);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)30);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(31,Loop,MS12+MS_Aft);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)31);
    			C.setCellStyle(TC.getCellStyle());
    			
    			this.setCellValue(32,Loop,MSD12);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)3);
				C = R.getCell((short)32);
    			C.setCellStyle(TC.getCellStyle());
    			
    			TFor = "SUM(H" + (Loop+1) + ",J" + (Loop+1) + ",L" + (Loop+1) + ",N" + (Loop+1) + ",P" + (Loop+1) + ",R" + (Loop+1) + ",T" + (Loop+1) + ",V" + (Loop+1) + ",X" + (Loop+1) + ",Z" + (Loop+1) +",AB" + (Loop+1) + ",AD" + (Loop+1) + ")";
    			this.set_CellFormula(33,Loop,TFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)33);
    			C.setCellStyle(TC.getCellStyle());    			
    			Loop=Loop+1;  
    			
//------------------- Total Starts--------------

    			GTFor_Stk = GTFor_Stk + "H" + (Loop+1) + "+";
    			GTFor_1 = GTFor_1 + "J" + (Loop+1) + "+";
    			GTFor_2 = GTFor_2 + "L" + (Loop+1) + "+";
    			GTFor_3 = GTFor_3 + "N" + (Loop+1) + "+";
    			GTFor_4 = GTFor_4 + "P" + (Loop+1) + "+";
    			GTFor_5 = GTFor_5 + "R" + (Loop+1) + "+";
    			GTFor_6 = GTFor_6 + "T" + (Loop+1) + "+";
    			GTFor_7 = GTFor_7 + "V" + (Loop+1) + "+";
    			GTFor_8 = GTFor_8 + "X" + (Loop+1) + "+";
    			GTFor_9 = GTFor_9 + "Z" + (Loop+1) + "+";
    			GTFor_10 = GTFor_10 + "AB" + (Loop+1) + "+";
    			GTFor_11 = GTFor_11 + "AD" + (Loop+1) + "+";
    			GTFor_12 = GTFor_12 + "AF" + (Loop+1) + "+";
    			GTFor_Tot = GTFor_Tot + "AH" + (Loop+1) + "+";
    			
    			this.setCellValue(5,Loop,"Total");
    			R=this.getRow((int)Loop);
    			TC = TR.getCell((short)0);
    			C = R.getCell((short)5);
    			C.setCellStyle(TC.getCellStyle());
    			
    			String CFor = ""; 
    			
    			CFor = "SUM(H" + (Loop-1) + ",H" + (Loop) + ")";
    			this.set_CellFormula(7,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)7);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(8,Loop,0);
    			R=this.getRow((int)Loop);    			
				TC = TR.getCell((short)5);
				C = R.getCell((short)8);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(J" + (Loop-1) + ",J" + (Loop) + ")";
    			this.set_CellFormula(9,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)9);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(10,Loop,0);
    			R=this.getRow((int)Loop);    			
				TC = TR.getCell((short)5);
				C = R.getCell((short)10);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(L" + (Loop-1) + ",L" + (Loop) + ")";
    			this.set_CellFormula(11,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)11);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(12,Loop,0);
    			R=this.getRow((int)Loop);    			
				TC = TR.getCell((short)5);
				C = R.getCell((short)12);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(N" + (Loop-1) + ",N" + (Loop) + ")";
    			this.set_CellFormula(13,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)13);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(14,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)14);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(P" + (Loop-1) + ",P" + (Loop) + ")";
    			this.set_CellFormula(15,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)15);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(16,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)16);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(R" + (Loop-1) + ",R" + (Loop) + ")";
    			this.set_CellFormula(17,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)17);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(18,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)18);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(T" + (Loop-1) + ",T" + (Loop) + ")";
    			this.set_CellFormula(19,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)19);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(20,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)20);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(V" + (Loop-1) + ",V" + (Loop) + ")";
    			this.set_CellFormula(21,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)21);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(22,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)22);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(X" + (Loop-1) + ",X" + (Loop) + ")";
    			this.set_CellFormula(23,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)23);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(24,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)24);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(Z" + (Loop-1) + ",Z" + (Loop) + ")";
    			this.set_CellFormula(25,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)25);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(26,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)26);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(AB" + (Loop-1) + ",AB" + (Loop) + ")";
    			this.set_CellFormula(27,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)27);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(28,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)28);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(AD" + (Loop-1) + ",AD" + (Loop) + ")";
    			this.set_CellFormula(29,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)29);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(30,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)30);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(AF" + (Loop-1) + ",AF" + (Loop) + ")";
    			this.set_CellFormula(31,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)31);
    			C.setCellStyle(TC.getCellStyle());

    			this.setCellValue(32,Loop,0);
    			R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)32);
    			C.setCellStyle(TC.getCellStyle());
    			
    			CFor = "SUM(AH" + (Loop-1) + ",AH" + (Loop) + ")";
    			this.set_CellFormula(33,Loop,CFor);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)33);
    			C.setCellStyle(TC.getCellStyle());
    			
    			Loop=Loop+2;    			
    			
//------------------- Total Ends----------------    			
			}
			
		}
			//Loop=Loop+1;
			GTFor_Stk = GTFor_Stk + "0";
			GTFor_1 = GTFor_1 + "0";
			GTFor_2 = GTFor_2 + "0";
			GTFor_3 = GTFor_3 + "0";
			GTFor_4 = GTFor_4 + "0";
			GTFor_5 = GTFor_5 + "0";
			GTFor_6 = GTFor_6 + "0";
			GTFor_7 = GTFor_7 + "0";
			GTFor_8 = GTFor_8 + "0";
			GTFor_9 = GTFor_9 + "0";
			GTFor_10 = GTFor_10 + "0";
			GTFor_11 = GTFor_11 + "0";
			GTFor_12 = GTFor_12 + "0";
			GTFor_Tot = GTFor_Tot + "0";
			
			this.setCellValue(3,Loop,"Grand Total");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)0);
			C = R.getCell((short)3);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(7,Loop,GTFor_Stk);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)7);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(8,Loop,0);
			R=this.getRow((int)Loop);    			
			TC = TR.getCell((short)7);
			C = R.getCell((short)8);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(9,Loop,GTFor_1);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)9);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(10,Loop,0);
			R=this.getRow((int)Loop);    			
			TC = TR.getCell((short)7);
			C = R.getCell((short)10);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(11,Loop,GTFor_2);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)11);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(12,Loop,0);
			R=this.getRow((int)Loop);    			
			TC = TR.getCell((short)7);
			C = R.getCell((short)12);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(13,Loop,GTFor_3);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)13);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(14,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)14);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(15,Loop,GTFor_4);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)15);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(16,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)16);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(17,Loop,GTFor_5);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)17);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(18,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)18);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(19,Loop,GTFor_6);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)19);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(20,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)20);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(21,Loop,GTFor_7);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)21);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(22,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)22);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(23,Loop,GTFor_8);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)23);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(24,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)24);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(25,Loop,GTFor_9);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)25);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(26,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)26);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(27,Loop,GTFor_10);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)27);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(28,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)28);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(29,Loop,GTFor_11);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)29);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(30,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)30);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(31,Loop,GTFor_12);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)31);
			C.setCellStyle(TC.getCellStyle());

			this.setCellValue(32,Loop,0);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)32);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(33,Loop,GTFor_Tot);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)7);
			C = R.getCell((short)33);
			C.setCellStyle(TC.getCellStyle());
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	  }
	  
}